#pragma strict
#pragma downcast
class AnimationHelper{
	/**
		包含containName的动画,播放加速.
		@param anim : animation
		@param ratio : speed
		@param containName : 是空或null时,则控制全部动画
		@isRecursion : 是否遍历全部的动画?
	*/
	static function Speed(anim:Animation,ratio:float,containName:String,isRecursion:boolean){
		if(anim != null){
			if(isRecursion){
				for (var stateObj : Object in anim) {
					var state:AnimationState = stateObj as AnimationState;
					if(String.IsNullOrEmpty(containName)){
						state.speed = ratio;
					}else{
						 if(state.name.Contains(containName)){
							state.speed = ratio;
						 }
					}
				}
			}else{
				if(anim[containName]){
					anim[containName].speed = ratio;
				}
			}
		}
	}
	/**
		全部的 动画速度控制.
	*/
	static function Speed(anim:Animation,ratio:float){
		Speed(anim,ratio,null,true);
	}
	/**
		播放.随机时刻.
	*/
	static function RandomTime(anim:Animation){
		if(anim != null){
			for(var stateObj : Object in anim){
				var state:AnimationState = stateObj as AnimationState;
				state.time = Random.value * state.length;
			}
		}
	}

	/**
		停止 某个含有name的动画
	*/
	static function StopAnim(anim:Animation,name:String){
		for (var stateObj : Object in anim) {
			var state:AnimationState = stateObj as AnimationState;
			if(!String.IsNullOrEmpty(name) && state.name.Contains(name)){
				state.normalizedTime = 0;
			}
		}
	}
	/**
		清除所有的动画剪辑
	*/
	static function Clear(anim:Animation){
		if(anim != null){
			var list:List.<String> = new List.<String>();
			for(var state:AnimationState in anim){
				list.Add(state.name);
			}
			list.ForEach(function(name:String){
				anim.RemoveClip(name);
			});
			list.Clear();
		}
	}
	/**
		添加动画简介.
	*/
	static function Add(anim:Animation,clips:AnimationClip[]){
		if(anim != null && clips != null){
			for(var clip:AnimationClip in clips){
				anim.AddClip(clip,clip.name);
			}
		}else{
			Debug.LogWarning("anim,clips为null");
		}
	}
	/**
		先全部去除,再添加.
	*/
	static function ClearAdd(anim:Animation,clips:AnimationClip[]){
		Clear(anim);
		Add(anim,clips);
	}
	/** 设置动画状态. */
	static function SetupState(state:AnimationState,wrapMode:WrapMode,blendMode:AnimationBlendMode,layer:int,speed:float,weight:float){
		if(state){
			state.wrapMode = wrapMode;
			state.blendMode = blendMode;
			state.layer = layer;
			state.speed = speed;
			state.weight = weight;
		}
	}
	static function SetupState(state:AnimationState,wrapMode:WrapMode){
		if(state)
			state.wrapMode = wrapMode;
	}
	static function SetupState(state:AnimationState,blendMode:AnimationBlendMode){
		if(state)
			state.blendMode = blendMode;
	}
	static function SetupState(state:AnimationState,layer:int){
		if(state)
			state.layer = layer;
	}
	static function SetupState(state:AnimationState,speed:float){
		if(state)
			state.speed = speed;
	}
}
